package com.bjsxt.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjsxt.commons.DataGridModel;
import com.bjsxt.mapper.DrugMapper;
import com.bjsxt.pojo.Drug;
import com.bjsxt.pojo.Sell;
import com.bjsxt.pojo.Users;
/**
 * 销售药品
 * @author 程应康
 *
 */
import com.bjsxt.service.SellMedicineService;

/**
 * 药品出售
 * 
 * @author 程应康
 *
 */
@Controller
@RequestMapping("/SellMedicine")
public class SellMedicine {
	@Autowired
	private SellMedicineService sellMedicineService;

	@RequestMapping("/findDrug")
	@ResponseBody
	public DataGridModel findDrug(Drug drug, @RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer rows) {
		DataGridModel dgm = null;
		if (drug.getDrugid() == null && drug.getDrugname() == null && drug.getDrugsal() == null
				&& drug.getDrugeffect() == null) {
			dgm = sellMedicineService.findDrug(page, rows);
			return dgm;
		} else {
			dgm = sellMedicineService.findDrug(drug, page, rows);
			return dgm;
		}

	}

	@RequestMapping("/sallDrug")
	@ResponseBody
	public Integer sallDrug(String drugids, String drugnames, String count, Integer grous, HttpSession session) {
		drugids = drugids.substring(1);
		drugnames = drugnames.substring(1);
		count = count.substring(1);
		Users users = (Users) session.getAttribute("users");
		Integer staffid = users.getStaffid();
		try {
			sellMedicineService.addsallDrug(drugids, drugnames, count, staffid);
			return 200;
		} catch (Exception e) {
			e.printStackTrace();
			return 500;
		}
	}

	@RequestMapping("/findsaleLog")
	@ResponseBody
	public DataGridModel findsaleLog(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer rows,
			Integer staffid,Integer drugid,String selldate) {
		System.out.println(staffid+"\t"+drugid+"\t"+selldate);
			Sell sell = new Sell();
			if (staffid!=null && staffid>0) {
				sell.setStaffid(staffid);
			}
			if (selldate!=null && selldate.length()>0) {
				sell.setSelldate(selldate);
			}
			if (drugid!=null && drugid>0) {
				sell.setDrugid(drugid);
			}
			
			if (sell.getDrugid()!=null||sell.getStaffid()!=null||sell.getSelldate()!=null) {
				return sellMedicineService.findsaleLog(page,rows,sell);
			} else {
				return sellMedicineService.findsaleLog(page, rows);
			}
	}

}
